# StartTCL: n
#
# Item Cooldown Script
#
# This program is (c) 2006 by Spirit <thehiddenspirit@hotmail.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. You can also consult
# the terms of the license at:
#
#               <http://www.gnu.org/copyleft/lesser.html>
#
# Name:         ItemCooldown.tcl
#
# Version:      0.20
#
# Date:         2006-06-06
#
# Description:  Cooldown for some item spells
#
# Author:       Spirit <thehiddenspirit@hotmail.com>
#
# Changelog:
#
#
# v0.20 (2006-06-06)
#	Cooldown group, succeed chance for Defibrillate.
#	Thanks to Ata for the suggestion.
#
# v0.11 (2006-05-06)
#	Item procedures for success and failed cases.
#
# v0.10 (2006-05-03)
#	Cooldown for Defibrillate (Goblin Jumper Cables).
#
#

# variables & initialization
namespace eval ::ItemCooldown {

	variable SAY 1

	variable VERSION 0.20
	variable USE_CONF 1

	# spellid { cooldown cooldown_group procedure (options) }
	variable ItemSpellData
	array set ItemSpellData {
		8342 { 1800 1051 Defibrillate 25 }
		22999 { 1800 1051 Defibrillate 50 }
	}

	if { $USE_CONF } { ::Custom::LoadConf }
	::Custom::AddSpellScript "::ItemCooldown::Check" [array names ItemSpellData]

	::StartTCL::Provide
}

# procedures
namespace eval ::ItemCooldown {

	# check whether a player can use a spell by checking for cooldown
	proc Check { to from spellid } {
		variable SAY
		variable LastUsedTime
		variable ItemSpellData
		set time [clock seconds]

		set cooldown [lindex $ItemSpellData($spellid) 0]
		set cooldown_group [lindex $ItemSpellData($spellid) 1]
		set proc [lindex $ItemSpellData($spellid) 2]
		set options [lrange $ItemSpellData($spellid) 3 end]

		if { !$cooldown_group } { set cooldown_group "group$spellid" }
		if { $proc == "" } { set proc "Default" }

		if { ![info exists LastUsedTime($from,$cooldown_group)] || [set timeleft [expr { $cooldown - $time + $LastUsedTime($from,$cooldown_group) }]] < 0 } {
			set can_use 1
			set LastUsedTime($from,$cooldown_group) $time
		} else {
			set can_use 0
			if { $SAY } { ::Say $from 0 [::Texts::Get "cant_use" [::Custom::SecondsToTime $timeleft]] }
		}

		$proc $to $from $spellid $can_use $options
	}

	# default procedure (kills the caster when failed)
	proc Default { to from spellid {can_use 1} {options ""} } {
		if { !$can_use } {
			::CastSpell $from $from 7
		}
	}

	# defibrillate
	proc Defibrillate { to from spellid {can_use 1} {options 0} } {
		variable SAY

		if { !$can_use } {
			return
		}

		# resurrect
		if { rand() * 100 <= $options } {
			::CastSpell $from $to 24341
		} elseif { $SAY } {
			::Say $from 0 [::Texts::Get "item_failed"]
		}
	}
}

# default localization
namespace eval ::ItemCooldown {
	if { ![::Texts::Exists] } {
		::Texts::Set en "cant_use" "Dieser Gegenstand ist noch nicht bereit! [::Custom::Color {Time left: %s} dcdcdc]."
		::Texts::Set en "item_failed" "Dieses Item ist fehlgeschlagen."
	}
}
